<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>药库管理</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<style type="text/css">
.item {
	margin: 4px
}
p{padding: 0;
 margin: 6px 5px;
}
</style>
</head>
<body>
	<script type="text/javascript">
		$.extend($.fn.datagrid.methods, {
			editCell : function(jq, param) {
				return jq.each(function() {
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields', true)
							.concat($(this).datagrid('getColumnFields'));
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field) {
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});

		var editIndex = undefined;
		function endEditing() {
			if (editIndex == undefined) {
				return true
			}
			if ($('#lfq-tb-sunyi').datagrid('validateRow', editIndex)) {
				$('#lfq-tb-sunyi').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field) {
			if (endEditing()) {
				$('#lfq-tb-sunyi').datagrid('selectRow', index).datagrid(
						'editCell', {
							index : index,
							field : field
						});
				editIndex = index;
			}
		}
	</script>
	<script type="text/javascript">
		$(function() {
			//刷新
			$("#lfq-purchasePlan-reload").linkbutton({
				onClick : function() {
					$("#lfq-tb-drug").datagrid({
						url : 'selectDrugAllDrug',
						method : 'post'
					});
					$('#lfq-purchasePlan-search').searchbox("setValue", "");
				}
			});

			//打开编辑采购计划的窗口
			$("#lfq-purchasePlan-edit").linkbutton(
					{
						onClick : function() {
							var rows = $("#lfq-tb-drug").datagrid(
									"getSelections");
							if (rows.length == 1) {
								$('#lfq-win-editDrug').window("open");
								//初始化表单
								var cpNo = rows[0].cpNo;
								$.getJSON("selectByIdDrug", {
									"cpNo" : cpNo
								}, function(data) {
									$("#lfq-form-editDrug").form("load", data);
								});

							} else {
								$.messager.alert("提示", "只能选择一条记录，您目前选择了"
										+ rows.length + "条!");
							}

						}
					});

			//编辑采购计划
			$("#lfq-btn-DrugOk").linkbutton({
				onClick : function() {
					$("#lfq-form-editDrug").form('submit', {
						url : 'eidtDrug',
						success : function(data) {
							$.messager.alert("提示", "修改成功！");
							$("#lfq-win-editDrug").window("close");
							$("#lfq-tb-drug").datagrid("reload");
						}
					});
				}
			});

			//根据条件搜索药品
			$('#lfq-purchasePlan-search').searchbox({
				searcher : function(value, name) {
					if (value == null || value.length == 0) {

					} else {
						if (name == "药品名称") {
							$("#lfq-tb-drug").datagrid({
								url : 'queryDrugLikeName',
								method : 'post',
								queryParams : {
									drugName : value
								}
							});
						} else if (name == "药品编号") {
							if (!isNaN(value)) {
								$("#lfq-tb-drug").datagrid({
									url : 'selectByIdDrugMap',
									method : 'post',
									queryParams : {
										cpNo : value
									}
								});
							} else {
								$.messager.alert("提示", "药品编号是由数字组成，请输入正确的编号！");
							}
						} else if (name == "药品类型") {
							if (value == "中" || value == "中药") {
								$("#lfq-tb-drug").datagrid({
									url : 'selectDrugAllZY',
									method : 'post'
								});
							} else if (value == "西" || value == "西药") {
								$("#lfq-tb-drug").datagrid({
									url : 'selectDrugAllXY',
									method : 'post'
								});
							}
						}
					}
				}
			});

			//双击打开的窗口
			$("#lfq-tb-drug").datagrid({
				onDblClickRow : function(rowIndex, rowData) {
					$.getJSON("selectByCpNoInventory", {
						"cpNo" : rowData.cpNo
					}, function(data) {
						$("#lfq-cpbh").html(data.kcNo);
						$("#lfq-ckbh").html(data.ckNo);
						$("#lfq-ypmc").html(data.kcName);
						$("#lfq-lssl").html(data.kcTotalnumber);
						$("#lfq-sysl").html(data.kcNumber);
						$("#lfq-dw").html(data.kcUnit);
						$("#lfq-yjz").html(data.kcWarning);
					});
					$("#lfq-win-shuangjiDrug").window("open");
				}
			});

			$("#lfq-btn-xgyjz").linkbutton(
					{
						onClick : function() {
							$("#lfq-win-yujinzhi").window("open");
							var yujinz = $("#lfq-textbox-yuJingzhi").textbox(
									"setValue", "");
						}
					});

			//修改预警值
			$("#lfq-btn-yuJingzhiOk").linkbutton(
					{
						onClick : function() {
							var cpNo = $("#lfq-cpbh").html();
							var yujinz = $("#lfq-textbox-yuJingzhi").textbox(
									"getValue");
							if (yujinz.length > 0) {
								$.getJSON("updataYuJingZhi", {
									"kcNo" : cpNo,
									"kcWarning" : yujinz
								}, function(data) {
									$("#lfq-yjz").html(yujinz);
									$.messager.alert("提示", data.reason);
									$("#lfq-win-yujinzhi").window("close");
								});
							}
						}
					});
			;

			//改变行内样式
			$('#lfq-tb-drug').datagrid({
				rowStyler : function(index, row) {
					if (row.cpNorms == null) {
						return 'color:red;';
					}
				}
			});
			
			//打开药品损益窗口
			$("#lfq-purchasePlan-sunyi").linkbutton({onClick:function(){
				$("#lfq-win-sunyi").window("open");
			}});
			
			//药品onhang事件
			$("#lfq-sunyiyaoping").combogrid({onChange:function(newValue, oldValue){
				$.getJSON("selectHisInventoryVolBykcNo", {
					"kcNo" : newValue
				},
				function(data) {
					$("#ypname3").textbox("setValue",data.kcName);
					$("#ypprice3").textbox("setValue",data.price);
					$("#kcnumber3").textbox("setValue",data.kcNumber);
					$("#sunyiWhy").textbox("setValue","");
					$("#lfq-sunyishuliang").numberbox("setValue","");
				});
			}});
			
			$("#lfq-sunyishuliang").numberbox({onChange:function(n,o){
				var kcnum=$("#kcnumber3").textbox("getValue");
				if(parseInt(n)>parseInt(kcnum)){
					$("#lfq-sunyishuliang").numberbox("setValue",kcnum);
				}else if(parseInt(n)<1){
					$("#lfq-sunyishuliang").numberbox("setValue","1");
				}
			}});
			
			//添加
			$("#lfq-btn-sunyiTianjia").linkbutton({onClick:function(){
				var a=$("#lfq-sunyishuliang").numberbox("getValue");
				var b=$("#kcnumber3").textbox("getValue");
				if(parseInt(a)>0 && parseInt(b)>0){
					var c=$("#ypname3").textbox("getValue");
					var d=$("#ypprice3").textbox("getValue");
					var e=$("#kcnumber3").textbox("getValue");
					var f=$("#sunyiWhy").textbox("getValue");
					
					var rows=$("#lfq-tb-sunyi").datagrid("getRows");
					if(rows.length>0){
						var k=0;
						for(var i=rows.length-1;i>=0;i--){
							if(rows[i].cpName==c){
								k++;
							}
						}
						if(k==0){
							$('#lfq-tb-sunyi').datagrid('appendRow', {
								'cpName' : [c],
								'cpPrice' : [d],
								'ckNumber' : [e],
								'sunyiWhy' : [f],
								'numbers' : [a]
							});
						}else{
							$.messager.alert("提示", "请不要重复添加！");
						}
					}else{
						$('#lfq-tb-sunyi').datagrid('appendRow', {
							'cpName' : [c],
							'cpPrice' : [d],
							'ckNumber' : [e],
							'sunyiWhy' : [f],
							'numbers' : [a]
						});
					}
				}
			}});
			
			//事件
			var sunyi=0;
			$("#lfq-tb-sunyi").datagrid({onBeforeEdit:function(index, row){
				sunyi=row.numbers;
			}});
			
			$("#lfq-tb-sunyi").datagrid({onAfterEdit:function(index, row, changes){
				if(parseInt(row.numbers)>parseInt(row.ckNumber)){
					$('#lfq-tb-sunyi').datagrid('updateRow',{
						index: index,
						row: {
							numbers: sunyi
						}
					});
					$.messager.alert("提示", "损益数量不能大于库存数量！请重新修改！");
				}
			}});
			
			$("#lfq-btn-sunyiOk2").linkbutton({onClick:function(){
				var rows=$("#lfq-tb-sunyi").datagrid("getRows");
				if(rows.length>0){
					var arrs=[];
					for(var i=0;i<rows.length;i++){
						arrs.push(rows[i].cpName);
						arrs.push(rows[i].cpPrice);
						arrs.push(rows[i].sunyiWhy);
						arrs.push(rows[i].numbers);
					}
					$('#lfq-form-sunyi').form('submit', {
						url : 'submitSunyi',
						onSubmit : function(param) {
							param.arr = arrs;
						},
						success : function(data) {
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
							//重新加载表格
							for(var i=rows.length-1;i>=0;i--){
								var ind=$("#lfq-tb-sunyi").datagrid("getRowIndex",rows[i]);
								$("#lfq-tb-sunyi").datagrid("deleteRow",ind);
							}
							$("#lfq-form-sunyi").form("clear")
							$("#lfq-win-sunyi").window("close");
						}
					});
				}
			}});

		});
	</script>

	<table id="lfq-tb-drug" class="easyui-datagrid"
		data-options="url:'selectDrugAllDrug',
			fitColumns:true,
			fit:true,
			singleSelect:true,
			nowrap:false,
			pagination: true,
			pageSize: 5,
			pageList: [5, 10, 15, 20],
			rownumbers: true,
			toolbar: '#lfq-tb1',
			border:false
			">
		<thead>
			<tr>
				<th data-options="field:'cpNo',width:'5%', checkbox: true">药品ID</th>
				<th data-options="field:'cpName',width:'10%'">药品名称</th>
				<th data-options="field:'cpType',width:'6%'">药品类型</th>
				<th data-options="field:'cpApproval',width:'7%'">批文编号</th>
				<th data-options="field:'cpUnit',width:'5%'">单位</th>
				<th data-options="field:'cpNorms',width:'5%'">规格</th>
				<th data-options="field:'cpPrice',width:'5%'">进价</th>
				<th data-options="field:'cpComposition',width:'13%'">主要成分</th>
				<th data-options="field:'cpFunction',width:'13%'">主治功能</th>
				<th data-options="field:'cpUsage',width:'13%'">使用方法</th>
				<th data-options="field:'cpTime',width:'7%'">生产日期</th>
				<th data-options="field:'cpValidity',width:'7%'">有效期</th>
				<th data-options="field:'kcNo',width:'5%'">产品ID</th>
			</tr>
		</thead>
	</table>

	<div id="lfq-tb1">
		<a id="lfq-purchasePlan-edit" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit',plain:true">编辑药品信息</a><a
			id="lfq-purchasePlan-sunyi" class="easyui-linkbutton"
			data-options="iconCls:'icon-sunyi',plain:true">药品损益</a> <a
			id="lfq-purchasePlan-reload" class="easyui-linkbutton"
			data-options="iconCls:'icon-reload',plain:true">刷新</a> <input
			id="lfq-purchasePlan-search" class="easyui-searchbox"
			data-options="menu: '#lfq-mm', width: 250, prompt: '请输入需要查询的信息'" />
	</div>

	<div id="lfq-mm" style="width: 120px">
		<div data-options="name:'药品名称'">药品名称</div>
		<div data-options="name:'药品编号'">药品编号</div>
		<div data-options="name:'药品类型'">药品类型</div>
	</div>

	<!-- 药品损益窗口 -->
	<div id="lfq-win-sunyi" class="easyui-window" title="药品损益"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 800px; height: 500px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'center'"
						style="padding: 10px; height: 38%;">
						<form id="lfq-form-sunyi" method="post">
							<p>
								<select id="lfq-sunyiyaoping" class="easyui-combogrid"
									style="width: 220px;"
									data-options="
											    label: '仓库药品',
											    panelWidth:150,
											    idField:'kcNo',
											    panelHeight:'170',
											    textField:'kcName',
											    url:'selectHisInventorylLikeBykcNoAndCpNorms',
											    columns:[[
											        {field:'kcName',title:'药品名称',width:140}
											    ]]
										    "></select>
							</p>
							<p>
								<input readonly="readonly" class="easyui-textbox"
									id="ypname3" data-options="label: '药品名称', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
								<input readonly="readonly" class="easyui-textbox"
									id="ypprice3" data-options="label: '药品价格', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
								<input readonly="readonly" class="easyui-textbox"
									id="kcnumber3" data-options="label: '药库数量', width: 200">
							</p>
							<p>
							    <input class="easyui-textbox"
									id="sunyiWhy" data-options="label: '损坏原因', width: '92%'">
							</p>
							<p>
								<input name="sunyiNumner" class="easyui-numberbox"
									id="lfq-sunyishuliang" data-options="label: '损益数量', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;
								<a id="lfq-btn-sunyiTianjia" class="easyui-linkbutton"
									data-options="width: 80">添加</a>
							</p>
						</form>
					</div>
					<div data-options="region:'south'"
						style="padding: 10px; height: 62%">
						<table id="lfq-tb-sunyi" class="easyui-datagrid"
							style="height: 226px"
							data-options="
								singleSelect: true,
								rownumbers: true,
								onClickCell: onClickCell,
								width: '100%'
							">
							<thead>
								<tr>
									<th data-options="field:'cpName',width:'25%'">药物名称</th>
									<th data-options="field:'cpPrice',width:'10%'">价格</th>
									<th data-options="field:'ckNumber',width:'10%'">仓库数量</th>
									<th data-options="field:'sunyiWhy',width:'40%',editor:'textbox'">损益原因</th>
									<th
										data-options="field:'numbers',width:'10%',editor:{type:'numberbox',options:{precision:0,min:1}}">损益数量</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a id="lfq-btn-sunyiOk2" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true">确定</a> <a
					class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel', plain:true"
					href="javascript:$('#lfq-win-sunyi').window('close');" onclick="">取消</a>
			</div>
		</div>
	</div>
	
	<!-- 双击行窗口 -->
	<div id="lfq-win-shuangjiDrug" class="easyui-window" title="库存情况"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 440px; height: 240px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<table cellpadding="5px 1px" cellspacing="5px" width="100%"
					style="font-size: 12px">
					<tbody>
						<tr>
							<td>产品编号：<span id="lfq-cpbh"></span></td>
							<td>仓库编号：<span id="lfq-ckbh"></span></td>
						</tr>
						<tr>
							<td>药品名称：<span id="lfq-ypmc"></span></td>
							<td>历史数量：<span id="lfq-lssl"></span></td>
						</tr>
						<tr>
							<td>剩余数量：<span id="lfq-sysl"></span></td>
							<td>单位：<span id="lfq-dw"></span></td>
						</tr>
						<tr>
							<td>预警值：<span id="lfq-yjz"></span></td>
							<td><a id="lfq-btn-xgyjz" class="easyui-linkbutton">修改预警值</a></td>
						</tr>
					</tbody>
				</table>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true"
					href="javascript:$('#lfq-win-shuangjiDrug').window('close');"
					onclick="">确定</a>
			</div>
		</div>
	</div>

	<!-- 修改预警值窗口 -->
	<div id="lfq-win-yujinzhi" class="easyui-window" title="修改预警值"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 300px; height: 126px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<input class="easyui-textbox" id="lfq-textbox-yuJingzhi"
					data-options="label:'请输入预警值', width:'80%'" />
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a id="lfq-btn-yuJingzhiOk" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true">确定</a> <a
					class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel', plain:true"
					href="javascript:$('#lfq-win-yujinzhi').window('close');"
					onclick="">取消</a>
			</div>
		</div>
	</div>

	<!-- 编辑药品信息窗口 -->
	<div id="lfq-win-editDrug" class="easyui-window" title="编辑药品信息"
		data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true"
		style="width: 620px; height: 420px; padding: 5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding: 10px;">
				<form id="lfq-form-editDrug" method="post">
					<div class="item">
						<input name="cpNo" readonly="readonly" class="easyui-textbox"
							data-options="label: '药品ID', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input name="cpName" class="easyui-textbox"
							data-options="label: '药品名称', width: 200, required:true">
					</div>
					<div class="item">
						药品类型 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="cpType"
							class="easyui-combobox" style="width: 140px"
							data-options="panelHeight: 'auto'">
							<option value="中药">中药</option>
							<option value="西药">西药</option>
						</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="cpApproval"
							class="easyui-textbox"
							data-options="label: '批文编号', width: 200, required:true">
					</div>
					<div class="item">
						单位 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select
							name="cpUnit" class="easyui-combobox" style="width: 140px"
							data-options="panelHeight: 'auto'">
							<option value="箱">箱</option>
							<option value="盒">盒</option>
							<option value="斤">斤</option>
							<option value="包">包</option>
							<option value="件">件</option>
							<option value="个">个</option>
							<option value="卷">卷</option>
						</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="cpNorms"
							class="easyui-textbox"
							data-options="label: '规格', width: 200, required:true">
					</div>
					<div class="item">
						<input name="cpPrice" class="easyui-textbox"
							data-options="label: '进价', width: 200, required:true">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input name="kcNo" readonly="readonly" class="easyui-textbox"
							data-options="label: '产品ID', width: 200">
					</div>
					<div class="item">
						<input name="cpTime" class="easyui-datebox"
							data-options="label: '生产日期', width: 200">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input name="cpValidity" class="easyui-datebox"
							data-options="label: '过期时间', width: 200">
					</div>
					<div class="item">
						<table class="lfq-kk" cellpadding="0" cellspacing="2">
							<tr>
								<td style="vertical-align: top;">主要成分&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
								<td><textarea name="cpComposition" rows="3" cols="50"></textarea></td>
							</tr>
							<tr>
								<td style="vertical-align: top">主治功能&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
								<td><textarea name="cpFunction" rows="3" cols="50"></textarea></td>
							</tr>
							<tr>
								<td style="vertical-align: top">使用方法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
								<td><textarea name="cpUsage" rows="3" cols="50"></textarea></td>
							</tr>
						</table>
					</div>
				</form>
			</div>
			<div data-options="region:'south',border:false"
				style="text-align: right; padding: 5px 0 0;">
				<a id="lfq-btn-DrugOk" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok', plain:true">确定</a> <a
					class="easyui-linkbutton"
					data-options="iconCls:'icon-cancel', plain:true"
					href="javascript:$('#lfq-win-editDrug').window('close');"
					onclick="">取消</a>
			</div>
		</div>
	</div>
</body>
</html>